package com.ancy.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ancy.aynch.LoginTask;
import com.ancy.aynch.RegisterTask;
import com.ancy.backupandroid.R;
import com.ancy.common.IError;

public class RegisterActivity extends Activity implements IError {

	Button signupButton;
	EditText usernameText;
	EditText passwordText;
	EditText repasswordText;
	EditText emailText;
	RelativeLayout progressLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		intializeUI();
	}

	void intializeUI() {
		progressLayout = (RelativeLayout) findViewById(R.id.progress_login_layout);
		signupButton = (Button) findViewById(R.id.btnSignUp);
		usernameText = (EditText) findViewById(R.id.txtUsername);
		passwordText = (EditText) findViewById(R.id.txtPassword);
		repasswordText = (EditText) findViewById(R.id.txtRePassword);
		emailText = (EditText) findViewById(R.id.txtEmail);

		signupButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				doSignup();
			}
		});
	}

	void doSignup() {
		if (doValidate()) {
			RegisterTask task = new RegisterTask(usernameText.getText()
					.toString().trim(), passwordText.getText().toString(),
					emailText.getText().toString(), progressLayout, this);
			task.execute();
		}
	}

	boolean doValidate() {

		if (usernameText.getText().toString().trim().equals("")) {
			Toast.makeText(getApplicationContext(), USERNAME_REQ,
					Toast.LENGTH_SHORT).show();
			return false;
		} else if (passwordText.getText().toString().trim().equals("")) {
			Toast.makeText(getApplicationContext(), PASSWD_REQ,
					Toast.LENGTH_SHORT).show();
			return false;
		} else if (repasswordText.getText().toString().trim().equals("")) {
			Toast.makeText(getApplicationContext(), REPASSWD_REQ,
					Toast.LENGTH_SHORT).show();
			return false;
		} else if (!repasswordText.getText().toString().trim()
				.equals(passwordText.getText().toString().trim())) {
			Toast.makeText(getApplicationContext(), PASSWD_MATCH,
					Toast.LENGTH_SHORT).show();
			return false;
		} else if (emailText.getText().toString().trim().equals("")) {
			Toast.makeText(getApplicationContext(), EMAIL_REQ,
					Toast.LENGTH_SHORT).show();
			return false;
		}

		return true;
	}
}
